package com.inkyi.assistant.core.system.service;

import com.inkyi.framework.dfs.DfsFileMate;
import com.inkyi.framework.dfs.DfsFileSession;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

@Service
public class InkDfsService {

    @Value("${ink.store.paht}")
    private String storePath;

    private DfsFileSession fileSession;

    @PostConstruct
    private void initSession() {
        fileSession = new DfsFileSession(storePath);
    }

    public DfsFileMate saveFile(String storageId, File file) {
        try {
            DfsFileMate dfsFileMate = fileSession.saveFile(storageId, file);
            return dfsFileMate;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void delFile(String storageId, String fileId) {
        try {
            fileSession.delFile(storageId, fileId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据文件id查找路径
     *
     * @param storageId 存储id
     * @param fileId    文件标识
     * @return {@link FileInputStream }
     * @throws FileNotFoundException 文件未发现异常
     */
    public String findPathByFileId(String storageId, String fileId) {
        String filePath = fileSession.findFilePath(storageId, fileId);
        return filePath;
    }
}
